using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using System.IO.IsolatedStorage;
using System.IO;
using System.Xml;

namespace FrameWork.Source.ReadWrite_class
{
    /// <summary>
    /// This is a singleton class. Can only has 1 instance of this class by using method getInstance()
    /// </summary>
    class CStorage
    {
        private static CStorage m_pStorage = null;

        /// <summary>
        /// Get Instance of this class
        /// </summary>
        /// <returns>An instance of this class</returns>
        public static CStorage getInstance()
        {
            if (m_pStorage == null)
            {
                m_pStorage = new CStorage();
            }

            return m_pStorage;
        }

        /// <summary>
        /// Constructor of this class
        /// </summary>
        private CStorage()
        {
            
        }

        /// <summary>
        /// Create new directory
        /// ex: SomePath//Game1//Memories...
        /// </summary>
        /// <param name="filePath">New directory</param>
        public void createDirectory(string filePath)
        {
            try
            {
                IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
                if (!string.IsNullOrEmpty(filePath) && !myIsolatedStorage.DirectoryExists(filePath))
                {
                    myIsolatedStorage.CreateDirectory(filePath);
                }
            }
            catch (Exception e)
            {

            }
        }

        /// <summary>
        /// Delete avaible directory
        /// ex: SomePath//Game1//Memories...
        /// </summary>
        /// <param name="filePath">Avaible directory</param>
        public void deleteDirectory(string filePath)
        {
            try
            {
                IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
                if (!string.IsNullOrEmpty(filePath) && myIsolatedStorage.DirectoryExists(filePath))
                {
                    myIsolatedStorage.DeleteDirectory(filePath);
                }
            }
            catch (Exception e)
            {
                
            }
        }

        /// <summary>
        /// Create new file
        /// Ex: SomePath//Game1//Memories//Save.txt ..
        /// Ex2 : SomePath//Game1//Memories//SaveXML.xml ...
        /// </summary>
        /// <param name="filePath">Path of file</param>
        public void createFile(string filePath)
        {
            try
            {
                IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
                if (!string.IsNullOrEmpty(filePath) && !myIsolatedStorage.FileExists(filePath))
                {
                    StreamWriter writer = new StreamWriter(new IsolatedStorageFileStream(filePath, FileMode.Create, myIsolatedStorage));
                }
            }
            catch (Exception e)
            {

            }
        }

        /// <summary>
        /// Delete current existance files
        /// Ex: SomePath//Game1//Memories//Save/txt ...
        /// </summary>
        /// <param name="filePath">Path of file</param>
        public void deleteFile(string filePath)
        {
            try
            {
                IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
                if (!string.IsNullOrEmpty(filePath) && myIsolatedStorage.FileExists(filePath))
                    myIsolatedStorage.DeleteFile(filePath);
            }
            catch (Exception e)
            {

            }
        }

        

        public void writeHighScore( string filePath , List<KeyValuePair<string,int>> pList )
        {
            try
            {
                using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    //if (myIsolatedStorage.FileExists(filePath))
                    //{
                    //    readHighScore(filePath, ref pList);
                    //}
                    using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile(filePath, FileMode.Create, FileAccess.Write))
                    {
                        using (StreamWriter writer = new StreamWriter(stream))
                        {
                            int size = pList.Count;
                            for (int i = 0; i < size; ++i)
                            {
                                writer.WriteLine(pList[i].Key);
                                writer.WriteLine(pList[i].Value.ToString());
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                
            }
        }

        public void readHighScore(string filePath, ref List<KeyValuePair<string, int>> pOutput)
        {
            try
            {
                using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (!myIsolatedStorage.FileExists(filePath))
                        return;
                    using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile(filePath, FileMode.Open, FileAccess.Read))
                    {
                        using (StreamReader reader = new StreamReader(stream))
                        {
                            while (!reader.EndOfStream)
                            {
                                string name = reader.ReadLine();
                                int score = int.Parse( reader.ReadLine() );

                                pOutput.Add(new KeyValuePair<string, int>(name, score));
                            }
                            stream.Close();
                            stream.Dispose();
                        }
                    }
                }
            }
            catch (Exception e)
            {

            }
        }
    }
}
